package com.zhenhao.xsq.ui.activity.video;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.sdk.NETDEV_DECODE_AUDIO_DATA_CALLBACK_PF;
import com.sdk.NETDEV_FILECOND_S;
import com.sdk.NETDEV_FINDDATA_S;
import com.sdk.NETDEV_PLAYBACKCOND_S;
import com.sdk.NETDEV_PREVIEWINFO_S;
import com.sdk.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S;
import com.sdk.NetDEVSDK;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.base.BaseActivity;
import com.zhenhao.xsq.netsdk_demo.CMainMenu;
import com.zhenhao.xsq.netsdk_demo.CPlayView;
import com.zhenhao.xsq.ui.activity.video.component.DaggerVideoComponent;
import com.zhenhao.xsq.ui.activity.video.contract.VideoContract;
import com.zhenhao.xsq.ui.activity.video.module.VideoModule;
import com.zhenhao.xsq.ui.activity.video.presenter.VideoPresenter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.inject.Inject;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sdk.NetDEVSDK.NETDEV_QueryVideoChlDetailListEx;

public class VideoActivity extends BaseActivity implements VideoContract.View, VideoSwitchListene {


    private String TAG = "VideoActivity";

    @Inject
    VideoPresenter mPresenter;

    @BindView(R.id.live_view)
    CPlayView liveView;
    @BindView(R.id.iv_video_play)
    ImageView ivVideoPlay;
    @BindView(R.id.iv_video_zoom)
    ImageView ivVideoZoom;
    @BindView(R.id.ll_qp_dh)
    LinearLayout llQpDh;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_pview)
    RelativeLayout flPview;
    @BindView(R.id.bt_live)
    Button btLive;
    @BindView(R.id.bt_play_back)
    Button btPlayBack;
    @BindView(R.id.bt_time)
    Button btTime;

    private int screenWidth, screenHeigh;

    public static float scaleRatio[] = new float[4];     //live video ID
    private long lpLiveView;//直播预览ID 默认为0,直播失败 成功后有值
    private long lpPlayback;//回放预览ID 默认为0,回放失败 成功后有值
    private int m_dwChannelID;
    private static boolean isLocalDevFlag;
    private static boolean isNoAccountDevFlag;
    private String deviceIp;
    private Long userID;
    public static int PlaybackStatus[] = new int[10];
    private NETDEV_PLAYBACKCOND_S m_stCurrentPlayInfo;    //information of current record file
    private int liveOrPlayback = 0;//直播或回放 1直播 2回放

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_video);
        ButterKnife.bind(this);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        // 保持屏幕常亮
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        autolayout.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        toolbarTitle.setTextColor(ContextCompat.getColor(this, R.color.color_333333));
        toolbarMenu.setTextColor(ContextCompat.getColor(this, R.color.color_2d97ff));
        mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.mipmap.toolbar_back2));
        setToolbarTitle("摄像头名称");

        getScreenWithHeigh();

        liveView.setmActivity(this);
        liveView.setVideoSwitchListene(this);

        final Intent intent = getIntent();
        ArrayList<String> szIPList = intent.getStringArrayListExtra("szIP");
        ArrayList<String> szDevNameList = intent.getStringArrayListExtra("szDevName");
        ArrayList<Long> lpUserIDList = (ArrayList<Long>) intent.getSerializableExtra("lpUserID");
        boolean bLocalDevFlag = intent.getBooleanExtra("bLocalDevFlag", false);
        boolean bNoAccountDevFlag = intent.getBooleanExtra("bNoAccountDevFlag", false);
        Log.e(TAG, "szIPList " + szIPList.toString());// [192.168.8.127]
        Log.e(TAG, "lpUserIDList " + lpUserIDList.toString());// [525.....]
        Log.e(TAG, "bLocalDevFlag " + bLocalDevFlag);// true
        Log.e(TAG, "bNoAccountDevFlag " + bNoAccountDevFlag);// false
        if (bNoAccountDevFlag) {
            isNoAccountDevFlag = bNoAccountDevFlag;
        }

        isLocalDevFlag = bLocalDevFlag;
        m_dwChannelID = 2;
        deviceIp = szIPList.get(0);
        userID = lpUserIDList.get(0);
        liveView.m_dwWinIndex = 1;

        ArrayList<NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S> list =
                new ArrayList<NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S>();
        Integer dwCount = 256;
        //查询视频通道信息列表
        NETDEV_QueryVideoChlDetailListEx(userID, dwCount, list);
        for (int i = 0; i < list.size(); i++) {
            NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S tmp = list.get(i);
            Log.e(TAG,"Channel - " + tmp.dwChannelID + ": Status -" + ((tmp.enStatus == 1) ? "Online" : "Offline") + "  Ptz -" + ((tmp.bPtzSupported != 0) ? "Yes" : "No"));

        }

        NetDEVSDK.gdwWinIndex = 1;

        LiveView();/*initial main interface*/

        PlaybackStatus[0] =
                NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_16_BACKWARD_IFRAME;
        PlaybackStatus[1] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_8_BACKWARD_IFRAME;
        PlaybackStatus[2] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_4_BACKWARD_IFRAME;
        PlaybackStatus[3] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_2_BACKWARD_IFRAME;
        PlaybackStatus[4] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_1_BACKWARD;
        PlaybackStatus[5] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_1_FORWARD;
        PlaybackStatus[6] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_2_FORWARD_IFRAME;
        PlaybackStatus[7] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_4_FORWARD_IFRAME;
        PlaybackStatus[8] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_8_FORWARD_IFRAME;
        PlaybackStatus[9] = NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_16_FORWARD_IFRAME;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setupActivityComponent() {
        DaggerVideoComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .videoModule(new VideoModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void setPresenter(VideoContract.VideoContractPresenter presenter) {
        mPresenter = (VideoPresenter) presenter;
    }

    @OnClick({R.id.iv_video_play, R.id.iv_video_zoom, R.id.iv_back, R.id.bt_live,
            R.id.bt_play_back, R.id.bt_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_video_play:
                if (liveOrPlayback == 1) {//直播
                    if (!liveView.m_bCanDrawFrame) {
                        livePlay();
                        openSound();
                    } else {
                        stopLive();
                        clsoeSound();
                    }
                } else if (liveOrPlayback == 2) {//回放
                    if (!liveView.m_bCanDrawFrame) {
                        playBack();
//                        openSound();
                    } else {
                        stopPlayBack();
//                        clsoeSound();
                    }
                }

                break;
            case R.id.iv_video_zoom:
                //切换全屏
                switchOrientation();
                break;
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.bt_live:
                stopPlayBack();
                clsoeSound();
                LiveView();
                break;
            case R.id.bt_play_back:
                NetDEVSDK.gdwWinIndex = 1;
                PlayBackView();
                break;
            case R.id.bt_time:
                queryRecord();
                break;
        }
    }

    private void queryRecord() {

        int dwFileNum = 0;

        NETDEV_FILECOND_S stFileCond = new NETDEV_FILECOND_S();

        SimpleDateFormat oDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date oBeginDate = null;
        Date oEndDate = null;
        String strBeginDate = "2024-09-20";//起始年月日 "yyyy-MM-dd"
        String strBeginTime = "01:01:01";//起始时分秒 "HH:mm:ss"
        String strEndDate = "2024-09-20";//终止年月日 "yyyy-MM-dd"
        String strEndTime = "02:02:02";//终止时分秒 "HH:mm:ss"

        /*As long as the input is empty, search the video for the last 24 hours by default*/
        if (strBeginDate.equals("") || strBeginTime.equals("") || strEndDate.equals("") || strEndTime.equals("")) {
            stFileCond.dwChannelID = m_dwChannelID;
            stFileCond.tBeginTime = (System.currentTimeMillis() / 1000) - (60 * 60 * 24);
            //search the video for the last 24 hours by default
            stFileCond.tEndTime = System.currentTimeMillis() / 1000;
        } else {
            try {
                oBeginDate = oDateFormat.parse(strBeginDate);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                oEndDate = oDateFormat.parse(strEndDate);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            stFileCond.dwChannelID = m_dwChannelID;
            stFileCond.tBeginTime =
                    oBeginDate.getTime() / 1000 + Integer.parseInt(strBeginTime.substring(0, 2)) * 3600 + Integer.parseInt(strBeginTime.substring(3, 5)) * 60 + Integer.parseInt(strBeginTime.substring(6));
            stFileCond.tEndTime =
                    oEndDate.getTime() / 1000 + Integer.parseInt(strEndTime.substring(0,
                            2)) * 3600 + Integer.parseInt(strEndTime.substring(3, 5)) * 60 + Integer.parseInt(strEndTime.substring(6));
        }
        Log.e(TAG, "stFileCond isLocalDevFlag " + isLocalDevFlag);
        Log.e(TAG, "stFileCond.dwChannelID " + stFileCond.dwChannelID);
        Log.e(TAG, "stFileCond.tBeginTime " + stFileCond.tBeginTime);
        Log.e(TAG, "stFileCond.tEndTime " + stFileCond.tEndTime);
        long dwFileHandle = 0;
        if (isLocalDevFlag) {
            //根据文件类型、时间查找设备录像文件
            //dwFileHandle为录像查询业务号，返回0表示失败
            //录像查询后必须调用NETDEV_FindClose函数，防止内存泄露
            dwFileHandle = NetDEVSDK.NETDEV_FindFile(userID, stFileCond);
        }
        Log.e(TAG, "dwFileHandle " + dwFileHandle);
        //int dwFileHandle = NetDEVSDK.NETDEV_FindFile(ipLpUserIDMap.get(strDevIP),
        // stFileCond);
        if (dwFileHandle == 0) {
//            Toast.makeText(CMainMenu.this, "Find file failed", Toast.LENGTH_SHORT).show();
//            System.out.println("find file failed");
            Log.e(TAG, "find file failed");
        } else {
            Log.e(TAG, "Find file success");
            final NETDEV_PLAYBACKCOND_S stPlaybackInfo = new NETDEV_PLAYBACKCOND_S();//按url录像回放结构体
            String[] szRecordFile = new String[1000];
            NETDEV_FINDDATA_S stVodFile = new NETDEV_FINDDATA_S();//录像查找信息
            for (int i = 0; i < 1000; i++) {
                //逐个获取查找到的文件信息
                int iRet = NetDEVSDK.NETDEV_FindNextFile(dwFileHandle, stVodFile);
                if (0 == iRet) {
                    Log.e(TAG, "find nextfile failed");
                    break;
                } else {
                    dwFileNum++;
                    NetDEVSDK.m_astVodFile[i] = new NETDEV_FINDDATA_S();
                    NetDEVSDK.m_astVodFile[i].szFileName = stVodFile.szFileName;
                    NetDEVSDK.m_astVodFile[i].tBeginTime = stVodFile.tBeginTime;
                    NetDEVSDK.m_astVodFile[i].tEndTime = stVodFile.tEndTime;
                    NetDEVSDK.m_astVodFile[i].byFileType = stVodFile.byFileType;
                    String strBeginLocalTime, strEndLocalTime;
                    Date nowTime = new Date(stVodFile.tBeginTime * 1000);
                    SimpleDateFormat oBeginDateFormat = new SimpleDateFormat("yyyy.MM.dd " +
                            "HH:mm:ss");
                    strBeginLocalTime = oBeginDateFormat.format(nowTime);
                    Date nowTime1 = new Date(stVodFile.tEndTime * 1000);
                    SimpleDateFormat oEndDataFormat = new SimpleDateFormat("yyyy.MM.dd " +
                            "HH:mm:ss");
                    strEndLocalTime = oEndDataFormat.format(nowTime1);
                    szRecordFile[i] = strBeginLocalTime + "---" + strEndLocalTime;
                    Log.e(TAG, strBeginLocalTime + "---" + strEndLocalTime);
                }
            }

            String[] szRecrodFileList;
            if (0 == dwFileNum) {
                szRecrodFileList = new String[1];
                szRecrodFileList[0] = "";
            } else {
                szRecrodFileList = new String[dwFileNum];
                System.arraycopy(szRecordFile, 0, szRecrodFileList, 0, dwFileNum);
                //first by default
                stPlaybackInfo.dwChannelID = m_stCurrentPlayInfo.dwChannelID =
                        m_dwChannelID;
                stPlaybackInfo.tBeginTime = m_stCurrentPlayInfo.tBeginTime =
                        NetDEVSDK.m_astVodFile[0].tBeginTime;
                stPlaybackInfo.tEndTime = m_stCurrentPlayInfo.tEndTime =
                        NetDEVSDK.m_astVodFile[0].tEndTime;
                stPlaybackInfo.dwLinkMode = m_stCurrentPlayInfo.dwLinkMode = 1;
                m_stCurrentPlayInfo = stPlaybackInfo;
            }

            AlertDialog.Builder oBuilder = new AlertDialog.Builder(VideoActivity.this);
            oBuilder.setTitle("Record File List");
            oBuilder.setCancelable(false);
            oBuilder.setSingleChoiceItems(szRecrodFileList, 0,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            stPlaybackInfo.dwChannelID = m_dwChannelID;
                            stPlaybackInfo.tBeginTime =
                                    NetDEVSDK.m_astVodFile[which].tBeginTime;
                            stPlaybackInfo.tEndTime =
                                    NetDEVSDK.m_astVodFile[which].tEndTime;
                            stPlaybackInfo.dwLinkMode = 1;
                        }
                    });

            oBuilder.setPositiveButton("select", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int i) {
                    m_stCurrentPlayInfo = stPlaybackInfo;
                    dialog.dismiss();
                }
            });
            oBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int i) {
                    dialog.dismiss();
                }
            });

            oBuilder.show();
        }

        //关闭文件查找，释放资源
        int iRet = NetDEVSDK.NETDEV_FindClose(dwFileHandle);
        if (1 == iRet) {
            Log.e(TAG, "find Close success");
        } else {
            Log.e(TAG, "find Close failed");
        }
    }

    private void livePlay() {
        Log.e(TAG, "播放");
        NETDEV_PREVIEWINFO_S stPreviewInfo = new NETDEV_PREVIEWINFO_S();
        stPreviewInfo.dwChannelID = m_dwChannelID;//  the value can be modified at the interface
        stPreviewInfo.dwLinkMode = 1;// 1,/* TCP */ 2,/* UDP */
        stPreviewInfo.dwStreamIndex = 0; // */       1,/*   Sub stream */       2,/*  Third
        // stream */

        Log.e(TAG, "播放1");
        liveView.m_bCanDrawFrame = true;
        //停止实时预览
        //lpPlayID 预览ID , dwWinIndex 窗口分屏索引
        NetDEVSDK.NETDEV_StopRealPlay(lpLiveView, liveView.m_dwWinIndex);


//                if (isLocalDevFlag) {
//                    Log.e(TAG, "播放5");
//                    //启动实时预览
//                    //lpUserID 用户登录ID
//                    //oPreviewInfo 预览参数
//                    //dwWinIndex 窗口分屏索引
//                    long  l = NetDEVSDK.NETDEV_RealPlay(ipLpUserIDMap.get(strDevIP),
//                    stPreviewInfo, NetDEVSDK.gdwWinIndex);
//                    lpLiveViewHandle[NetDEVSDK.gdwWinIndex - 1] = l;
//                }

        //获取实况码流URL
        String strStreamUrl = NetDEVSDK.NETDEV_GetStreamUrl(NetDEVSDK.lpUserID,
                m_dwChannelID, 0);
        Log.e(TAG, "strStreamUrl " + strStreamUrl);//rtsp://192.168.8
        // .127:554/unicast/c2/s0/live
        Log.e(TAG, "NetDEVSDK.lpUserID " + NetDEVSDK.lpUserID);//525342392320
        Log.e(TAG,
                "stPreviewInfo " + stPreviewInfo.dwChannelID + " , " + stPreviewInfo.dwStreamIndex + " , " + stPreviewInfo.dwLinkMode);//2 , 0 , 1
        Log.e(TAG, "NetDEVSDK.gdwWinIndex " + NetDEVSDK.gdwWinIndex);//1
        if (strStreamUrl.isEmpty()) {
            return;
        }

        //按照url启动实时预览
        long l = NetDEVSDK.NETDEV_FastRealPlayByUrl(NetDEVSDK.lpUserID, strStreamUrl,
                stPreviewInfo, NetDEVSDK.gdwWinIndex);
        lpLiveView = l;
        Log.e(TAG, "播放6 " + l);


        scaleRatio[NetDEVSDK.gdwWinIndex - 1] = 1.0f;//比例尺
        //调节数字放大
        //scaleRatio 比例尺 , X 横坐标 , y 纵坐标 , dwWinIndex 窗口分屏索引
        NetDEVSDK.Scale(scaleRatio[NetDEVSDK.gdwWinIndex - 1], 0, 0, NetDEVSDK.gdwWinIndex);
    }

    private void stopLive() {
        int iRet = -1;
        if (true == liveView.m_bCanDrawFrame) {
            liveView.m_bCanDrawFrame = false;
            iRet = NetDEVSDK.NETDEV_StopRealPlay(lpLiveView, liveView.m_dwWinIndex);
        }

        lpLiveView = 0;
        Log.e(TAG, "stopLive----------------------" + iRet);
    }

    private void playBack() {
        liveView.m_bCanDrawFrame = true;
        NetDEVSDK.NETDEV_StopPlayBack(lpPlayback, liveView.m_dwWinIndex);

        m_stCurrentPlayInfo.dwPlaySpeed =
                NetDEVSDK.NETDEV_VOD_PLAY_STATUS_E.NETDEV_PLAY_STATUS_1_FORWARD;

        if (isLocalDevFlag) {
            lpPlayback = NetDEVSDK.NETDEV_PlayBackByTime(userID, m_stCurrentPlayInfo,
                    NetDEVSDK.gdwWinIndex);
        }

        if (0 == lpPlayback) {
            System.out.println("Play the video failed.");
        }
        NetDEVSDK.NETDEV_SetPictureFluency(lpPlayback, 1);
        String strStreamUrl = NetDEVSDK.NETDEV_GetReplayUrl(NetDEVSDK.lpUserID,
                m_dwChannelID, 0);//rtsp://192.168.8.127:554/c2/b1726592461/e1726607105
        Log.e(TAG, "strStreamUrl " + strStreamUrl);
        if (strStreamUrl.isEmpty()) {
            return;
        }
        lpPlayback = NetDEVSDK.NETDEV_FastPlayBackByUrl(NetDEVSDK.lpUserID, strStreamUrl,
                m_stCurrentPlayInfo, NetDEVSDK.gdwWinIndex);
        Log.e(TAG, "播放 " + lpPlayback);
        if (0 == lpPlayback) {
            System.out.println("Play the video failed.");
        }
    }

    private void stopPlayBack() {
        int iRet = -1;
        if (true == liveView.m_bCanDrawFrame) {
            liveView.m_bCanDrawFrame = false;
            iRet = NetDEVSDK.NETDEV_StopPlayBack(lpPlayback, liveView.m_dwWinIndex);
        }

        lpPlayback = 0;
        Log.e(TAG, "stopPlayBack----------------------" + iRet);
    }

    private void openSound() {
        NETDEV_DECODE_AUDIO_DATA_CALLBACK_PF pfDecodeAudioCallBack =
                new NETDEV_DECODE_AUDIO_DATA_CALLBACK_PF();

        long lUserData = 0;
        if (isLocalDevFlag) {
            isLocalDevFlag = true;
            lUserData = userID;
        }
        NetDEVSDK.NETDEV_Android_SetPlayDecodeAudioCB(lpLiveView, pfDecodeAudioCallBack, 0,
                lUserData);
        NetDEVSDK.NETDEV_OpenSound(lpLiveView);
    }

    private void clsoeSound() {
        NetDEVSDK.NETDEV_CloseSound(lpLiveView);
        long lUserData = 0;
        if (isLocalDevFlag) {
            isLocalDevFlag = true;
            lUserData = userID;
        }
        NetDEVSDK.NETDEV_Android_SetPlayDecodeAudioCB(lpLiveView, null, 0, lUserData);
    }

    /**
     * 切换视频全屏/小视频窗口(以切横竖屏切换替代)
     */
    private void switchOrientation() {
        // 横竖屏切换
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
            //横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            //竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

    }

    @Override
    public void onVideoSwitch(int i) {
        Log.e(TAG, "屏幕大小发生变化");
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
            //竖屏
            Log.e(TAG, "竖屏");
            //手机导航栏
            int Heigh = screenWidth * 9 / 16;
            ConstraintLayout.LayoutParams parames = new ConstraintLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            parames.height = Heigh;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    autolayout.setVisibility(View.VISIBLE);
                    llQpDh.setVisibility(View.VISIBLE);
                    ivBack.setVisibility(View.GONE);
                    Log.e(TAG, "Width " + parames.width + " Heigh " + Heigh);
                    flPview.setLayoutParams(parames);
                }
            });


        } else if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            //横屏
            Log.e(TAG, "横屏");
            //显示全屏导航

            ConstraintLayout.LayoutParams parames = new ConstraintLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    autolayout.setVisibility(View.GONE);
                    ivBack.setVisibility(View.VISIBLE);
                    llQpDh.setVisibility(View.GONE);
                    Log.e(TAG, "Width " + parames.width + " Heigh " + parames.height);
                    flPview.setLayoutParams(parames);//调整画布容器宽高(比例)
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            super.onBackPressed();
        }
    }

    public void getScreenWithHeigh() {
        DisplayMetrics dm = new DisplayMetrics();
        // 获取屏幕信息
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeigh = dm.heightPixels;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void LiveView() {
        liveOrPlayback = 1;
        if (0 != lpPlayback) {
            NetDEVSDK.NETDEV_StopPlayBack(lpPlayback, liveView.m_dwWinIndex);
            lpPlayback = 0;
        }
        liveView.m_bCanDrawFrame = false;
    }

    private void PlayBackView() {
        liveOrPlayback = 2;
//        m_oPlaySpeed.setText(" ");
        NetDEVSDK.NETDEV_StopRealPlay(lpLiveView, liveView.m_dwWinIndex);

        liveView.m_bCanDrawFrame = false;

        m_stCurrentPlayInfo = new NETDEV_PLAYBACKCOND_S();

        /* pause */
        /*m_oPauseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                m_oPlaySpeed.setText("  ");
                long dwlpUserID = 0;
                if (isLocalDevFlag) {
                    dwlpUserID = ipLpUserIDMap.get(strDevIP);
                }
                if ((lpPlaybackPlayerHandle[NetDEVSDK.gdwWinIndex - 1] != 0) && (dwlpUserID != 0)) {
                    NETDEV_PLAYBACKCONTROL_S PlayBackControlenSpeed =
                            new NETDEV_PLAYBACKCONTROL_S();
                    int iRet =
                            NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
                            .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
                            .NETDEV_PLAY_CTRL_PAUSE, PlayBackControlenSpeed);

                    if (1 != iRet) {
                        System.out.println("pause play failed.");
                    }
                }
            }
        });*/

        /* Resume */
        /*m_oResumeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                m_oPlaySpeed.setText("  ");
                long dwlpUserID = 0;
                if (isLocalDevFlag) {
                    dwlpUserID = ipLpUserIDMap.get(strDevIP);
                }
                if ((lpPlaybackPlayerHandle[NetDEVSDK.gdwWinIndex - 1] != 0) && (dwlpUserID != 0)) {
                    NETDEV_PLAYBACKCONTROL_S PlayBackControlenSpeed =
                            new NETDEV_PLAYBACKCONTROL_S();
                    int iRet =
                            NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
                            .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
                            .NETDEV_PLAY_CTRL_RESUME, PlayBackControlenSpeed);
                    if (1 != iRet) {
                        System.out.println("pause play failed.");
                    }
                }
            }
        });*/

        /* forward */
//        m_oForwardBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                NETDEV_PLAYBACKCONTROL_S stPlaybackCtrl = new NETDEV_PLAYBACKCONTROL_S();
//
//                int iRet =
//                        NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
//                        .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
//                        .NETDEV_PLAY_CTRL_GETPLAYTIME, stPlaybackCtrl);
//                if (0 == iRet) {
//                    System.out.println("Get play time failed.");
//                } else {
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    long timeLong = Long.valueOf(stPlaybackCtrl.enSpeed);
//                    String strTime = sdf.format(new Date(timeLong * 1000L));
//                    System.out.println("Get play time :" + strTime);
//                }
//
//                iRet = NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
//                .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
//                .NETDEV_PLAY_CTRL_GETPLAYSPEED, stPlaybackCtrl);
//                if (0 == iRet) {
//                    System.out.println("Get play time failed.");
//                } else {
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    long timeLong = Long.valueOf(stPlaybackCtrl.enSpeed);
//                    String strTime = sdf.format(new Date(timeLong * 1000L));
//                    System.out.println("Get play time :" + strTime);
//                }
//
//                int i = 0;
//                for (; i < 10; i++) {
//                    if (PlaybackStatus[i] == stPlaybackCtrl.enSpeed) {
//                        break;
//                    }
//                }
//                i++;
//                if (i > 9) {
//                    i = 0;
//                }
//
//                switch (i) {
//                    case 0:
//                        m_oPlaySpeed.setText("-16×");
//                        break;
//                    case 1:
//                        m_oPlaySpeed.setText("-8×");
//                        break;
//                    case 2:
//                        m_oPlaySpeed.setText("-4×");
//                        break;
//                    case 3:
//                        m_oPlaySpeed.setText("-2×");
//                        break;
//                    case 4:
//                        m_oPlaySpeed.setText("-1×");
//                        break;
//                    case 5:
//                        m_oPlaySpeed.setText("1×");
//                        break;
//                    case 6:
//                        m_oPlaySpeed.setText("2×");
//                        break;
//                    case 7:
//                        m_oPlaySpeed.setText("4×");
//                        break;
//                    case 8:
//                        m_oPlaySpeed.setText("8×");
//                        break;
//                    case 9:
//                        m_oPlaySpeed.setText("16×");
//                        break;
//                    default:
//                        break;
//                }
//
//                stPlaybackCtrl.enSpeed = PlaybackStatus[i];
//                iRet = NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
//                .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
//                .NETDEV_PLAY_CTRL_SETPLAYSPEED, stPlaybackCtrl);
//                if (0 == iRet) {
//                    System.out.println("Set play speed failed.");
//                }
//            }
//        });

        /* backward */
//        m_oBackwardBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                NETDEV_PLAYBACKCONTROL_S PlayBackControlenSpeed = new NETDEV_PLAYBACKCONTROL_S();
//                PlayBackControlenSpeed.enSpeed = 0;
//                int iRet =
//                        NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
//                        .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
//                        .NETDEV_PLAY_CTRL_GETPLAYSPEED, PlayBackControlenSpeed);
//                if (0 == iRet) {
//                    System.out.println("Get play speed failed.");
//                }
//
//                int i = 0;
//                for (; i < 10; i++) {
//                    if (PlaybackStatus[i] == PlayBackControlenSpeed.enSpeed) {
//                        break;
//                    }
//                }
//                i--;
//                if (i < 0) {
//                    i = 9;
//                }
//
//                switch (i) {
//                    case 0:
//                        m_oPlaySpeed.setText("-16×");
//                        break;
//                    case 1:
//                        m_oPlaySpeed.setText("-8×");
//                        break;
//                    case 2:
//                        m_oPlaySpeed.setText("-4×");
//                        break;
//                    case 3:
//                        m_oPlaySpeed.setText("-2×");
//                        break;
//                    case 4:
//                        m_oPlaySpeed.setText("-1×");
//                        break;
//                    case 5:
//                        m_oPlaySpeed.setText("1×");
//                        break;
//                    case 6:
//                        m_oPlaySpeed.setText("2×");
//                        break;
//                    case 7:
//                        m_oPlaySpeed.setText("4×");
//                        break;
//                    case 8:
//                        m_oPlaySpeed.setText("8×");
//                        break;
//                    case 9:
//                        m_oPlaySpeed.setText("16×");
//                        break;
//                    default:
//                        break;
//                }
//                PlayBackControlenSpeed.enSpeed = PlaybackStatus[i];
//                iRet = NetDEVSDK.NETDEV_PlayBackControl(lpPlaybackPlayerHandle[NetDEVSDK
//                .gdwWinIndex - 1], NetDEVSDK.NETDEV_VOD_PLAY_CTRL_E
//                .NETDEV_PLAY_CTRL_SETPLAYSPEED, PlayBackControlenSpeed);
//                if (0 == iRet) {
//                    System.out.println("Set play speed failed.");
//                }
//            }
//
//        });

    }
}
